<?php  
class ControllerCommonHome extends Controller {
	public function index() {
		$this->language->load('common/home');
		
		$this->document->title = $this->config->get('config_title');
		$this->document->description = "noi that nam dai phat";
		
		$this->data['heading_title'] = sprintf($this->language->get('heading_title'), $this->config->get('config_name'));
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('setting/store');
		$this->load->model('catalog/product');
		$this->load->model('catalog/category');
		$this->load->model('tool/image');
		$this->load->model('catalog/information');
		$this->load->model('tool/seo_url');  
		
		if (!$this->config->get('config_store_id')) {
			$this->data['welcome'] = html_entity_decode($this->config->get('config_description_' . $this->config->get('config_language_id')), ENT_QUOTES, 'UTF-8');
		} else {
			$store_info = $this->model_setting_store->getStore($this->config->get('config_store_id'));
			
			if ($store_info) {
				$this->data['welcome'] = html_entity_decode($store_info['description'], ENT_QUOTES, 'UTF-8');
			} else {
				$this->data['welcome'] = '';
			}
		}
						
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/home.tpl';
		} else {
			$this->template = 'default/template/common/home.tpl';
		}
		
		$this->children = array();
		
		$this->load->model('checkout/extension');
		
		$module_data = $this->model_checkout_extension->getExtensionsByPosition('module', 'home');
		
		$this->data['modules'] = $module_data;
		
		foreach ($module_data as $result) {
			$this->children[] = 'module/' . $result['code'];
		}
		
		$this->children[] = 'common/column_right';
		//$this->children[] =	'common/column_left';
		$this->children[] =	'common/footer';
		$this->children[] =	'common/header';
		/*----------------STart set Data for View -----------------*/
		
		if (isset($this->request->get['page'])) 
		{
			$page = $this->request->get['page'];
		} else 
		{ 
			$page = 1;
		}	
		
		$lastest_product = $this->model_catalog_product->getLatestProducts(9);
		
		if(isset($lastest_product))
		{
			$i = 0;
			$temp = null;
			
			foreach($lastest_product as $last)
			{
				$category_con=$this->model_catalog_category->getcategorybyproduct($last['product_id']);
				$category_cha=$this->model_catalog_category->getcategorycha($category_con);
				$category = "";
				if($category_cha==0)
				{
					$category = $category_con;
				}
				else 
				{
					$category = $category_cha."_".$category_con;
				}
				if ($last['image']) {
						$image = $last['image'];
					} else {
						$image = 'no_image.jpg';
					}
				$temp[$i] = array(
				"id" => $last['product_id'],
				"name" => $last['name'],
				'thumb'   => $this->model_tool_image->resize($image, 175, 175),
				'image'	  => 'image/'.$image,
				"description" =>  substr(html_entity_decode($last['description']),0,500)."...",
				"model" => $last['model'],
				"price" => $last['price'],
				"link" => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&path='.$category.'&product_id='.$last['product_id']),
				);
				$i++;
			}
			$this->data['lastest']=$temp;
		}
		$limit = 5;
		$information = $this->model_catalog_information->getLimitInformations(($page - 1)*$limit,$limit);
		$information_total = $this->model_catalog_information->getTotalProductsByCategoryId();
		if(isset($information))
		{
			$i = 0;
			$temp1 = null;
			foreach($information as $info)
			{
				$temp1[$i] = array(
				"id" => $info['new_id'],
				"title" => $info['title'],
				"description" => html_entity_decode($info['short_des']), 
				"create_date" => $this->time->makeDays($info['create_date']),
				"image" =>$this->timhinh(html_entity_decode($info['description'])),
				"href" =>  $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=news/detail&new_id='.$info['new_id']),
				);
				$i++;
			}
			$this->data['information']=$temp1;

			$pagination = new Pagination();
			$pagination->total = $information_total;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = "";
			$pagination->url = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home&page={page}');
			$this->data['pagination'] = $pagination->render();	
		}
		$intro= $this->model_catalog_information->getInformation(6);
		
		$temp2 = array(
		'title' =>$intro['title'],
		'content' =>html_entity_decode($intro['description']),
		);
		$this->data['intro']=$temp2;
		$this->data['link_product'] =  $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&path=');
		$this->data['template'] = $this->config->get('config_template');
		$this->data['link_tintuc'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=news/news');
		
		/*----------------End set Data for View -----------------*/
		
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	public function timhinh($text)
	{
		preg_match('/ src=\"(.*?)\"/',$text,$arr);
		if($arr==null)
		{
			$image= "catalog/view/theme/default/image/no_image.jpg";
		}
		else $image = $arr[1];
		return $image;
	}
}
?>